<template>
  <div class="col main-content">
    <div class="row tool-bar">
      <div class="col">
        <div>
          <div class="order-search-item">
            <div class="label">
              采购单号:
            </div>
            <div class="content">
              <el-input v-model="search.purchaseSn" placeholder="请输入系统采购单号" clearable>
              </el-input>
            </div>
          </div>
          <div class="order-search-item">
            <div class="label">
              买家:
            </div>
            <div class="content">
              <h-user-choose v-model="search.buyerId"></h-user-choose>
            </div>
          </div>
          <div class="order-search-item">
            <div class="label">
              付款编码:
            </div>
            <div class="content">
              <el-input v-model="search.paySn" placeholder="请输入订单的付款码" clearable>
              </el-input>
            </div>
          </div>

          <div class="order-search-item">
            <div class="label">
              商品货号:
            </div>
            <div class="content">
              <h-item-no-choose v-model="search.itemNo"/>
            </div>
          </div>
          <div class="order-search-item-small">
            <div class="label">
              订单类型:
            </div>
            <div class="content">
              <h-order-type-select v-model="search"></h-order-type-select>
            </div>
          </div>

          <div class="order-search-item-small">
            <div class="label">
              成交价格:
            </div>
            <div class="content">
              <el-input v-model="search.cost" placeholder="最终成交价格" clearable></el-input>
            </div>
          </div>
          <div class="order-search-item-small">
            <div class="label">
              发货状态:
            </div>
            <div class="content">
              <h-dispatch-status v-model="search"></h-dispatch-status>
            </div>
          </div>
          <div class="order-search-item-small">
            <div class="label">
              付款状态:
            </div>
            <div class="content">
              <h-pay-status v-model="search"></h-pay-status>
            </div>
          </div>
          <div class="order-search-item-small">
            <div class="label">
              到货状态:
            </div>
            <div class="content">
              <h-arrival-status v-model="search"></h-arrival-status>
            </div>
          </div>

          <div class="order-search-item-query">
            <div class="label">
              规格:
            </div>
            <div class="content">

              <h-spec-query v-for="item in search.specs"
                            :key="item"
                            :value="item"></h-spec-query>
            </div>
          </div>
          <div class="order-search-item">
            <div class="label">
              订单编号:
            </div>
            <div class="content">
              <el-input v-model="search.orderSn" placeholder="请输入与系统订单号" clearable>
              </el-input>
            </div>
          </div>
          <div class="order-search-item">
            <div class="label">
              订单日期:
            </div>
            <div style="display: inline-block">
              <h-date-range-picker v-model="search.orderData"/>
            </div>
          </div>
        </div>
        <div>
          <div class="inline-item">
            <el-button type="primary" @click="searchAction"><i class="fa fa-search"></i> 查询</el-button>
          </div>
          <div class="inline-item">
            <div class="label inline">
              排序:
            </div>
            <div class="inline">
              <el-radio-group v-model="orderName" @change="orderChange">
                <el-radio :label="2">按降序</el-radio>
                <el-radio :label="1">按升序</el-radio>
              </el-radio-group>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col">
        <el-table
          :data="datas"
          border
          tooltip-effect="dark"
          v-loading="loading"

          @selection-change="handleSelectionChange"
          ref="table"
        >
          <el-table-column
            type="selection"
            width="48" fixed="left">
          </el-table-column>
          <el-table-column label="订单编号" width="130" align="center">
            <template slot-scope="scope">
              <div>{{scope.row.sn}}</div>
              <div v-if="scope.row.supplement==1" class="red">
                (补运费订单)
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="买家"
            width="200">
            <template slot-scope="scope">
              <h-user-display v-model="scope.row.buyer"></h-user-display>
            </template>
          </el-table-column>
          <el-table-column
            label="商品信息"
            min-width="280">
            <template slot-scope="scope">
              <h-good-info-display v-model="scope.row" :admin="true"/>
            </template>
          </el-table-column>
          <el-table-column
            label="商品规格"
            min-width="100"
          >
            <template slot-scope="scope">
              <h-specs-display v-model="scope.row"></h-specs-display>
            </template>
          </el-table-column>

          <el-table-column label="订单信息" min-width="200">
            <template slot-scope="scope">
              <h-order-info-display v-model="scope.row"/>
            </template>
          </el-table-column>
          <el-table-column
            label="订单金额"
            min-width="150">
            <template slot-scope="scope">
              <h-order-pay-info-display v-model="scope.row"/>
            </template>
          </el-table-column>
          <el-table-column
            label="状态信息" width="120" align="center">
            <template slot-scope="scope">
              <h-order-status-display v-model="scope.row" :apply="true" :dispatch="true" :arrival="true"
                                      :pay="true"></h-order-status-display>
            </template>
          </el-table-column>
          <el-table-column
            label="备注" min-width="120" prop="note">
          </el-table-column>

          <el-table-column fixed="right"
                           label="订单操作" width="100">
            <template slot-scope="scope">
              <div class="text-center">
                <div>
                  <el-button type="text" @click="changePay(scope.$index,scope.row)">
                    对账
                  </el-button>
                </div>
                <div>
                  <el-button v-if="scope.row.supplement==0" type="text" @click="changeArrival(scope.$index,scope.row)">
                    货物状态
                  </el-button>
                </div>
                <div v-if="scope.row.supplement==0">
                  <el-button type="text" @click="freightCost(scope.$index,scope.row)">
                    <div class="mb-15">补运费</div>
                    <div class="red" v-if="scope.row.makeUp>0">已补运费({{scope.row.makeUp}})</div>
                  </el-button>
                </div>
              </div>
            </template>
          </el-table-column>


          <el-table-column fixed="right"
                           label="操作" align="center" width="100">
            <template slot-scope="scope">
              <!-- div>
                <el-tooltip placement="top" v-if="scope.row.pay==0">
                  <div slot="content">当用户未付款时可以对该订单进行编辑操作</div>
                  <el-button
                    type="text"
                    @click="deleteOrder(scope.$index,scope.row)">
                    编辑
                  </el-button>
                </el-tooltip>

              </div -->
              <div>
                <el-tooltip placement="top" v-if="scope.row.pay==0">
                  <div slot="content">当用户未付款时可以对该订单进行删除操作</div>
                  <el-button
                    type="text" class="red"
                    @click="deleteOrder(scope.$index,scope.row)">
                    删除
                  </el-button>
                </el-tooltip>

              </div>
            </template>
          </el-table-column>


        </el-table>
        <div class="col h-pagination">
          <div v-if="datas.length>0" class="pagination-tool">
            <!--<el-button type="primary" @click="payOrders" :loading="payloading">已付款</el-button>-->
            <el-button type="primary" @click="arrivalOrders(2)" :loading="arrivalLoading">已到货</el-button>
            <el-button type="danger" @click="arrivalOrders(0)" :loading="arrivalLoading">缺货</el-button>
          </div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="search.pagination.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="search.pagination.total">
          </el-pagination>
        </div>
      </div>
    </div>

    <h-pay-status-dialog :show.sync="payShow" title="变更付款状态" v-model="currentData"/>
    <h-arrival-status-dialog :show.sync="arrivalShow" title="变更到货状态" v-model="currentData"/>
    <h-freight-pay-dialog :show.sync="freightCostShow" title="添加补运费单" @success="makeUp"
                          v-model="currentData"></h-freight-pay-dialog>
  </div>
</template>


<script>
  import AdminService from 'src/service/admin'
  import HDateRangePicker from 'src/components/common/HDateRangePicker'
  import HItemNoChoose from 'src/components/common/HItemNoChoose'
  import HUserChoose from 'src/components/common/HUserChoose'
  import TableMix from 'src/components/table-mix'
  import HOrderTypeSelect from 'src/components/common/select/HOrderTypeSelect'
  import HArrivalStatus from 'src/components/common/HArrivalStatus'
  import HDispatchStatus from 'src/components/common/HDispatchStatus'
  import HPayStatus from 'src/components/common/HPayStatus'
  import HPayStatusDialog from 'src/components/admin/orders/components/HPayStatusDialog'
  import HArrivalStatusDialog from 'src/components/admin/orders/components/HArrivalStatusDialog'
  import HOrderStatusDisplay from 'src/components/common/display/HOrderStatusDisplay'
  import HUserDisplay from 'src/components/common/display/HUserDisplay'
  import HSpecsDisplay from 'src/components/common/display/HSpecsDisplay'
  import HOrderPayInfoDisplay from 'src/components/common/display/HOrderPayInfoDisplay'
  import HOrderInfoDisplay from 'src/components/common/display/HOrderInfoDisplay'
  import HGoodInfoDisplay from 'src/components/common/display/HGoodInfoDisplay'
  import HFreightPayDialog from 'src/components/admin/orders/components/HFreightPayDialog'
  import HSpecQuery from 'src/components/common/choose/HSpecQuery'

  export default {
    name: 'AdminOrder',
    mixins: [TableMix],
    components: {
      HGoodInfoDisplay,
      HOrderInfoDisplay,
      HOrderPayInfoDisplay,
      HDateRangePicker, HItemNoChoose, HUserChoose, HOrderTypeSelect,
      HArrivalStatus, HDispatchStatus, HPayStatus,
      HPayStatusDialog,
      HSpecQuery,
      HArrivalStatusDialog,
      HOrderStatusDisplay,
      HUserDisplay,
      HSpecsDisplay,
      HFreightPayDialog
    },
    data() {
      return {
        orderName: 2,
        datas: [],
        payloading: false,
        arrivalLoading: false,
        search: {
          orderSn: null,
          purchaseSn: null,
          arrival: null,
          paySn: null,
          orderType: null,
          dispatch: null,
          pay: null,
          status: 1,
          orderDate: [],
          itemNo: null,
          buyerId: null,
          cost: null,
          pagination: {
            total: 0,
            page: 1,
            limit: 10
          },
          specs: [{
            name: null,
            value: null
          }],
          orderBy: 'sn desc'
        },
        payShow: false,
        arrivalShow: false,
        currentData: null,
        freightCostShow: false,
        loading: false,
        selectRows: []
      }
    },
    watch: {
      'search.orderType': function (n, o) {
        this.searchOrder();

      },
      'search.dispatch': function (n, o) {
        this.searchOrder();

      },
      'search.pay': function (n, o) {
        this.searchOrder();

      },
      'search.arrival': function (n, o) {
        this.searchOrder();
      }
    }
    ,
    methods: {
      orderChange: function (v) {
        if (v == 1) {
          this.search.orderBy = "sn";
        } else {
          this.search.orderBy = "sn desc";
        }
        return this.searchOrder();
      },
      payOrders: function () {
        const me = this
        const data = {
          ids: this.selectIds(),
          status: 1
        }
        me.payloading = true
        AdminService.orderChangePayStatus(data).then(d => {
          if (d != null) {
            this.searchOrder();
            me.$message.success('修改商品付款状态成功!')
          }
          me.payloading = false
        })
      },
      makeUp: function (data) {
        this.freightCostShow = false
        this.searchOrder();
        this.$message.success('添加补运费单成功!')
      },
      arrivalOrders: function (status) {
        const me = this
        const data = {
          ids: this.selectIds(),
          status: status
        }
        me.arrivalLoading = true
        AdminService.orderChangeArrivalStatus(data).then(d => {
          if (d != null) {
            this.searchOrder();
            me.$message.success('修改商品到货状态成功!')
          }
          me.arrivalLoading = false
        })
      },
      selectIds: function () {
        return this.selectRows.map(d => {
          return d.id
        })
      },
      freightCost: function (index, data) {
        const me = this
        me.currentData = data
        me.freightCostShow = true
      },
      changeArrival: function (index, data) {
        const me = this
        this.currentData = data
        this.arrivalShow = !this.arrivalShow
        setTimeout(function () {
          me.arrivalShow = true
        }, 200)
      },
      handleSelectionChange: function (row) {
        this.selectRows = row
      },
      changePay: function (index, data) {
        const me = this
        this.currentData = data
        setTimeout(function () {
          me.payShow = true
        }, 200)
      },
      deleteOrder: function (index, data) {
        const me = this
        this.$confirm('此操作将删除该订单且不可恢复，是否删除?', '提示', {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => {
          AdminService.deleteOrder(data.id).then(ds => {
            if (ds != null) {
              me.$message.success('删除订单【' + data.sn + '】成功。')
              me.searchOrder()
            }
          })

        }).catch(() => {

        })
      },
      searchAction: function () {
        this.search.pagination.page = 1
        this.searchOrder()
      },
      //查询数据
      searchOrder: function () {
        const me = this
        console.info(this.search);
        me.loading = true
        AdminService.orderSearch(this.search).then(datas => {
          if (datas != null) {
            me.datas = datas.datas
            me.search.pagination = datas.pagination
          }
          me.$nextTick(function () {
            me.loading = false
          })
        })

      },
      handleCurrentChange: function (page) {
        this.search.pagination.page = page
        this.searchOrder()
      },
      handleSizeChange: function (size) {
        this.search.pagination.limit = size
        this.search.pagination.page = 1
        this.searchOrder()
      }
    },
    mounted: function () {
      // this.initTalbeHeight(340)
      this.searchOrder()
    }
  }
</script>
